import 'package:flutter/material.dart';
import 'package:tab_indicator_styler/tab_indicator_styler.dart';

class MyTabHeader extends StatelessWidget implements PreferredSizeWidget {
  final TabController tabController;
  final List<String> tabs;
  final Function(int) onTap;

  const MyTabHeader({
    super.key,
    required this.tabController,
    required this.tabs,
    required this.onTap,
  });

  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context).colorScheme;
    return Container(
      height: 30,
      width: double.infinity,
      color: theme.primaryContainer,
      child: TabBar(
        physics: const ClampingScrollPhysics(), // 滚动边界回弹效果
        controller: tabController,
        labelColor: theme.primary,
        indicatorPadding: EdgeInsets.all(2),
        dividerHeight: 0, // 去除 TabBar 下方的分割线
        tabs: tabs.map((e) => Tab(text: e)).toList(),
        indicatorColor: theme.primary, // 指示器颜色
        unselectedLabelColor:
            theme.onSurface, // 未选中标签颜色
        // 自定义样式
        indicator: MaterialIndicator(
          height: 3,
          topRightRadius: 10,
          topLeftRadius: 10,
          bottomLeftRadius: 10,
          bottomRightRadius: 10,
          horizontalPadding: 0,
          color: theme.primary,
        ),
        onTap: onTap,
      ),
    );
  }

  @override
  Size get preferredSize => const Size.fromHeight(kToolbarHeight);
}
